﻿using CoffeeShop.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CoffeeShop.DAO
{
    public class UserDao
    {
        private CFMSEntities _entities = new CFMSEntities();

        /// <summary>
        /// Get a User by UserName
        /// </summary>
        /// <param name="userName">userName: UserName input</param>
        /// <returns>User</returns>
        public User GetUserByUserName(string userName)
        {
            try
            {
                var selectUser = (from user in _entities.Users
                                  where user.UserName == userName
                                        && user.IsDeleted == false
                                  select user).FirstOrDefault();
                return selectUser;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// Check UserName is duplicate or not
        /// </summary>
        /// <param name="userName"></param>
        /// <returns>True: Duplicate -- False: Not duplicate</returns>
        public bool IsDuplicateUserName(string userName)
        {
            try
            {
                return _entities.Users.Any(user => user.UserName == userName);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                throw ex;
            }
        }

        /// <summary>
        /// Add new a user into DB
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool AddNewUser(User user)
        {
            try
            {
                _entities.Users.Add(user);
                var result = _entities.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }
    }
}